/****************************************************************************

MIT License

Copyright (c) 2021 唐佐林
WeChat : delphi_tang
EMail: delphi_tang@dt4sw.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*****************************************************************************/

#include <malloc.h>
#include "wifiiot_errno.h"
#include "utility.h"
#include "i2c_impl.h"

#define IDX_MAX  2

static void I2CResetFunc0(void);
static void I2CPrepareFunc0(void);
static void I2CRestoreFunc0(void);
static void I2CResetFunc1(void);
static void I2CPrepareFunc1(void);
static void I2CRestoreFunc1(void);

typedef struct 
{
    mp_obj_t reset;
    mp_obj_t prepare;
    mp_obj_t restore;
} mp_custom_func_t;

static const int BUF_SIZE = 256;

static mp_custom_func_t g_custom_func[] = 
{
    {mp_const_none, mp_const_none, mp_const_none},
    {mp_const_none, mp_const_none, mp_const_none},
};

static WifiIotI2cFunc g_func[] = 
{
    {I2CResetFunc0, I2CPrepareFunc0, I2CRestoreFunc0},
    {I2CResetFunc1, I2CPrepareFunc1, I2CRestoreFunc1},
};

static void call(mp_obj_t func)
{
    if( func != mp_const_none )
    {
        mp_call_function_0(func);
    }
}

static void I2CResetFunc0(void)
{
    call(g_custom_func[0].reset);
}

static void I2CPrepareFunc0(void)
{
    call(g_custom_func[0].prepare);
}

static void I2CRestoreFunc0(void)
{
    call(g_custom_func[0].restore);
}

static void I2CResetFunc1(void)
{
    call(g_custom_func[1].reset);
}

static void I2CPrepareFunc1(void)
{
    call(g_custom_func[1].prepare);
}

static void I2CRestoreFunc1(void)
{
    call(g_custom_func[1].restore);
}

mp_obj_t mp_i2c_init(mp_obj_t idx, mp_obj_t baudrate)
{
    int id = mp_obj_get_int(idx);
    unsigned int br = mp_obj_get_int(baudrate);
    unsigned int r = I2cInit((WifiIotI2cIdx)id, br);
    
    if( (r == WIFI_IOT_SUCCESS) && (0 <= id) && (id < IDX_MAX) )
    {
        I2cRegisterResetBusFunc((WifiIotI2cIdx)id, g_func[id]);
    }
    
    return mp_obj_new_int(r);
}

mp_obj_t mp_i2c_deinit(mp_obj_t idx)
{
    int id = mp_obj_get_int(idx);
    unsigned int r = I2cDeinit((WifiIotI2cIdx)id);
    
    if( (r == WIFI_IOT_SUCCESS) && (0 <= id) && (id < IDX_MAX) )
    {
        g_custom_func[id].reset = mp_const_none;
        g_custom_func[id].prepare = mp_const_none;
        g_custom_func[id].restore = mp_const_none;
    }
    
    return mp_obj_new_int(r);
}

mp_obj_t mp_i2c_write(mp_obj_t idx, mp_obj_t addr, mp_obj_t data)
{
    unsigned int r = -1;
    int id = mp_obj_get_int(idx);
    int devAddr = mp_obj_get_int(addr);  
    unsigned char* bytes = NULL;
    size_t len = pylist_to_integer_array(data, bytes);
    
    if( bytes )
    {
        WifiIotI2cData tosend = {0};
        
        tosend.sendBuf = bytes;
        tosend.sendLen = len;
        
        r = I2cWrite((WifiIotI2cIdx)id, (unsigned short)devAddr, &tosend);
    }
    
    free(bytes);
    
    return mp_obj_new_int(r);
}

mp_obj_t mp_i2c_read(mp_obj_t idx, mp_obj_t addr)
{
    unsigned int r = -1;
    int id = mp_obj_get_int(idx);
    int devAddr = mp_obj_get_int(addr);  
    unsigned char* buffer = malloc(sizeof(*buffer) * BUF_SIZE);
    mp_obj_t ret[2] = 
    {
        mp_obj_new_int(-1),
        mp_const_none,
    };
    
    if( buffer )
    {
        WifiIotI2cData toread = {0};
        mp_obj_t pylist = mp_const_none;
        
        toread.receiveBuf = buffer;
        toread.receiveLen = BUF_SIZE;
        
        r = I2cRead((WifiIotI2cIdx)id, (unsigned short)devAddr, &toread);
        
        if( (r == WIFI_IOT_SUCCESS) && ((pylist = integer_array_to_pylist(toread.receiveBuf, toread.receiveLen)) != mp_const_none) )
        {           
            ret[0] = mp_obj_new_int(r);
            ret[1] = pylist;
        }
    }
    
    free(buffer);
    
    return mp_obj_new_tuple(2, ret);
}

mp_obj_t mp_i2c_write_read(mp_obj_t idx, mp_obj_t addr, mp_obj_t data)
{
    unsigned int r = -1;
    int id = mp_obj_get_int(idx);
    int devAddr = mp_obj_get_int(addr);  
    unsigned char* bytes = NULL;
    size_t len = pylist_to_integer_array(data, bytes);
    unsigned char* buffer = malloc(sizeof(*buffer) * BUF_SIZE);
    mp_obj_t ret[2] = 
    {
        mp_obj_new_int(-1),
        mp_const_none,
    };
        
    if( bytes && buffer )
    {
        WifiIotI2cData towr = {0};
        mp_obj_t pylist = mp_const_none;
        
        towr.sendBuf = bytes;
        towr.sendLen = len;
        towr.receiveBuf = buffer;
        towr.receiveLen = BUF_SIZE;
        
        r = I2cWriteread((WifiIotI2cIdx)id, (unsigned short)devAddr, &towr);
        
        if( (r == WIFI_IOT_SUCCESS) && ((pylist = integer_array_to_pylist(towr.receiveBuf, towr.receiveLen)) != mp_const_none) )
        {           
            ret[0] = mp_obj_new_int(r);
            ret[1] = pylist;
        }
    }
    
    free(bytes);
    free(buffer);
    
    return mp_obj_new_tuple(2, ret);
}

mp_obj_t mp_i2c_set_baudrate(mp_obj_t idx, mp_obj_t baudrate)
{
    int id = mp_obj_get_int(idx);
    unsigned int br = mp_obj_get_int(baudrate);  
    unsigned int r = I2cSetBaudrate((WifiIotI2cIdx)id, br);
    
    return mp_obj_new_int(r);
}

mp_obj_t mp_i2c_set_reset_func(mp_obj_t idx, mp_obj_t func)
{
    int id = mp_obj_get_int(idx);
    unsigned int r = 0;
    
    if( (0 <= id) && (id < IDX_MAX) )
    {
        g_custom_func[id].reset = func;
    }
    else
    {
        r = -1;
    }
    
    return mp_obj_new_int(r);
}

mp_obj_t mp_i2c_set_prepare_func(mp_obj_t idx, mp_obj_t func)
{
    int id = mp_obj_get_int(idx);
    unsigned int r = 0;
    
    if( (0 <= id) && (id < IDX_MAX) )
    {
        g_custom_func[id].prepare = func;
    }
    else
    {
        r = -1;
    }
    
    return mp_obj_new_int(r);
}

mp_obj_t mp_i2c_set_restore_func(mp_obj_t idx, mp_obj_t func)
{
    int id = mp_obj_get_int(idx);
    unsigned int r = 0;
    
    if( (0 <= id) && (id < IDX_MAX) )
    {
        g_custom_func[id].restore = func;
    }
    else
    {
        r = -1;
    }
    
    return mp_obj_new_int(r);
}
